package com.zto.sch.base.thread;

public class ThreadDemo_Join {
    /**
     * 假设有两个线程，一个是线程 A，另一个是线程 B，两个线程分别依次打印 1-3 三个数字
     *
     * @param args
     */
    public static void main(String[] args) {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                printNum("Thread-A");
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    threadA.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                printNum("Thread-B");
            }
        });
        threadA.start();
        threadB.start();
    }

    private static void printNum(String threadName) {
        int i = 0;
        while (i++ < 13) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(threadName + " print: " + i);
        }

    }
}